<template>
  <div>
     <!--表单-->
     <div>
       <el-form ref="form" :model="role" label-width="100px">
            <el-form-item label="角色编码" >
                <el-input v-model="role.roleCoding"></el-input>
            </el-form-item>
            <el-form-item label="角色名称">
                <el-input v-model="role.roleName"></el-input>
            </el-form-item>
            <el-form-item label="创建时间">
                <el-input v-model="role.createTime"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="toAddUser">添加</el-button>
                <el-button>取消</el-button>
            </el-form-item>
       </el-form>
     </div>
     <!--列表-->
     <div>
          <el-table
            ref="userTable"
            :data="roleList"
            highlight-current-row
            style="width: 100%">
            <el-table-column
            type="index"
            width="50">
            </el-table-column>
            <el-table-column
            property="roleCoding"
            label="角色编码"
            width="120">
            </el-table-column>
            <el-table-column
            property="roleName"
            label="角色名称"
            width="120">
            </el-table-column>
            <el-table-column
            property="createTime"
               width="300"
            label="创建时间">
            </el-table-column>
            <el-table-column
      fixed="right"
      label="操作"
      width="300">
      <template slot-scope="scope">
       <el-button type="primary" size="small" @click="handleEdit(scope)">修改</el-button>
       <el-button type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
      </template>
    </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'修改用户':'新增用户'">
        <el-form ref="form" :model="role" label-width="100px">
            <el-form-item label="角色编码" >
                <el-input v-model="role.roleCoding"></el-input>
            </el-form-item>
            <el-form-item label="角色名称">
                <el-input v-model="role.roleName"></el-input>
            </el-form-item>
            <el-form-item label="创建时间">
                <el-input v-model="role.createTime"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="toAddUser">添加</el-button>
                <el-button>取消</el-button>
            </el-form-item>
       </el-form>    
       
    
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmUser">确定</el-button>
      </div>
    </el-dialog>

     </div>
  </div>
</template>

<script>
import {addUser,searchUsers,deleteUserRestfull,editRole} from "@/api/user"

export default {
  data(){
      return {
          role:{},
          roleList:[
              {
                  roleCoding:'lotus',
                  roleName:'12345',
                  createTime:'忘忧啊'
              },
              {
                 roleCoding:'lotus',
                  roleName:'12345',
                  createTime:'忘忧啊'
              }
          ]
      }
  },
  methods:{
    toAddUser(){
      console.log(this.role)
      addUser(this.role).then(res=>{
        this.searchUsers()
      })
    },
    searchUsers(){
      searchUsers().then(res=>{
        this.roleList =res.data
        console.log(this.roleList)
      })
    },
        handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
     
      this.role=deepClone(scope.row)
    },
      async confirmUser() {
        const isEdit = this.dialogType === 'edit'
           // 处理修改
        if (isEdit) {
           await editRole(this.role)

             for(let i=0;i<this.roleList.length;i++){
              if(this.roleList[i].urId===this.user.urId){
                this.roleList.splice(i,1,this.user)
                break
              }
             }

        }
   handleDelete({ $index, row }) {
      this.$confirm('是否删除该用户?', 'Warning', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          deleteUserRestfull(row.id)
          this.roleList.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功~..~!'
          })
        })
        .catch(err => { console.error(err) })
    }
  },
  created(){
    this.searchUsers()
  }
}
</script>

<style>

</style>